package com.hzhk.happyrent.ui.activity.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.view.ClearEditText;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NickNameChangeActivity extends AppCompatActivity {

    @BindView(R.id.activity_titlebar_left)
    ImageView activityTitlebarLeft;
    @BindView(R.id.activity_titlebar_center)
    TextView activityTitlebarCenter;
    @BindView(R.id.act_user_name)
    ClearEditText actUserName;
    @BindView(R.id.act_login_submit)
    Button actLoginSubmit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nick_name_change);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        activityTitlebarCenter.setText(R.string.nick_name_change);
        if(!TextUtils.isEmpty(getIntent().getStringExtra("nickname"))){
            actUserName.setText(getIntent().getStringExtra("nickname"));
        }
    }

    @OnClick({R.id.activity_titlebar_left, R.id.act_login_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_titlebar_left:
                finish();
                break;
            case R.id.act_login_submit:
                initData();
                break;
        }
    }

    private void initData() {
        if(TextUtils.isEmpty(actUserName.getText().toString())){
            Toast.makeText(NickNameChangeActivity.this,"请输入昵称",Toast.LENGTH_SHORT).show();
            return ;
        }else if(getIntent().getStringExtra("nickname").equals(actUserName.getText().toString())){
            Toast.makeText(NickNameChangeActivity.this,"昵称未修改",Toast.LENGTH_SHORT).show();
            return ;
        }
        Map<String,Object> params=new HashMap<>();
        params.put("id", MyPerference.getInstance().getUserId());
        params.put("nickname", actUserName.getText().toString());
        HttpManager.getInstance().perform_post(API.EDIT_USER_INFO, params, new MyStringResponse(this) {
            @Override
            public void onError(Object tag, Throwable e) {
                Toast.makeText(NickNameChangeActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onCancel(Object tag, Throwable e) {

            }
            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel=new Gson().fromJson(response,CodeModel.class);
                if (0==codeModel.getErrCode()){
                    Toast.makeText(NickNameChangeActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    getIntent().putExtra("nickname",actUserName.getText().toString());
                    setResult(RESULT_OK, getIntent());
                    finish();
                }
            }
        });
    }
}
